Visual Studio 2017环境配置MPI v9.0 并行编程环境

第一步:下载安装mpi 官网:http://www.mpich.org/ windows版官网:https://msdn.microsoft.com/en-us/library/bb524831(v=vs.85).aspx

​​​​​​enter image description here
点击下载后选择不同操作系统的版本这里选择windows
enter image description here
enter image description here
者都勾选下载,下载后分别点击安装msmpisdk.msi与MsMpiSetup.exe(根据提示安装即可)

第二步:Visual Studio 2017配置环境

  1. 新建项目->windows 桌面向导->选择控制台应用程序(勾选空项目)

enter image description here
enter image description here

2.源文件->添加新建项->C++文件

3.配置项目属性

包含目录:加入mpi SDK中的include文件夹
库目录:加入mpi SDK中的lib和lib/X64文件夹
enter image description here
enter image description here
C/C++->预处理器->预处理器定义->加入MPICH_SKIP_MPICXX(防止警告)
enter image description here
链接器->输入->附加依赖项
enter image description here
编译完成后,进入程序生成的.exe文件所在的文件夹,命令行运行.exe程序

mpiexec -n 4 Project8.exe
enter image description here

##

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

void Hello(void);

int main(int argc, char *argv[])
{
int me, option, namelen, size;
char process_name[MPI_MAX_PROCESSOR_NAME];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &size);

if (size < 2) {
fprintf(stderr, "system requires at least 2 processes");
MPI_Abort(MPI_COMM_WORLD, 1);
}
MPI_Get_processor_name(process_name, &namelen);
fprintf(stderr, "Process %d is alive on %s\n", me, process_name);
MPI_Barrier(MPI_COMM_WORLD);
Hello();
MPI_Finalize();
}

void Hello()
{
int nproc, me;
int type = 1;
int buffer[2], node;
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
if (me==0) {
printf("\nHello test from all to all\n");
fflush(stdout);
}
for(node = 0; node < nproc; node++)
{
if (node != me) {
buffer[0] = me;
buffer[1] = node;
MPI_Send(buffer, 2, MPI_INT, node, type, MPI_COMM_WORLD);
MPI_Recv(buffer, 2, MPI_INT, node, type, MPI_COMM_WORLD, &status);
if (buffer[0] != node || buffer[1] != me) {
fprintf(stderr, "Hello: %d != %d or %d != %d\n", buffer[0], node, buffer[1], me);
printf("Mismatch on hello process ids; node = %d\n",node);
}
printf("Hello from %d to %d\n",me,node);
fflush(stdout);
}
}
}

× 请我吃糖~
打赏二维码